﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Wpf.CommonTools.Converter
{
    [Serializable]
    public class EnumModel : IEquatable<int>, IEquatable<object>, IEquatable<EnumModel>, IEquatable<string>
    {
        public string Name { get; set; }
        public int Value { get; set; }

        public override bool Equals(object obj)
        {
            EnumModel s = obj as EnumModel;
            if (s != null)
            {
                return this.Equals(s);
            }
            else if (obj is int)
            {
                return this.Equals((int)obj);
            }
            else
            {
                return this.Equals(obj.ToString());
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return this.Name;
        }

        #region IEquatable<int> 成员

        public bool Equals(int other)
        {
            return Value == other;
        }

        #endregion

        #region IEquatable<EnumModel> 成员

        public bool Equals(EnumModel other)
        {
            return this.Equals(other.Value);
        }

        #endregion

        #region IEquatable<string> 成员

        public bool Equals(string other)
        {
            return this.Name == other;
        }

        #endregion
    }
    public class EnumToIntConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((EnumModel)value).Value;
        }

        #endregion
    }
}
